import React, { Component } from 'react'
import { Card, Form, Input, Button, Checkbox } from 'antd'
import logo from 'assets/logo.png'
import styles from './index.module.scss'
import store from 'redux/index'
export default class Login extends Component {
  state = {
    isLogining: false,
  }
  // 正则表达式 复习
  // 量词  + -- 1-n    * -- 0-n   ? --  0-1
  // /d 数字(0-9)  /w 大小写字母数字_
  // 17227282@qq.com
  // 邮箱 正则  /^\w+@\w+\.\w+$/
  // 17227282@qq.com.cn
  //  正则  /^\w+@\w+(\.\w)+$/
  render() {
    console.log(this.props)
    return (
      <div className={styles.login}>
        <Card className="login-container">
          <img className="login-logo" src={logo} alt="" />
          <Form
            name="basic"
            initialValues={{ remember: true }}
            autoComplete="off"
            size="large"
            onFinish={this.onFinish}
            initialValues={{
              mobile: '13911111111',
              code: '246810',
              agree: true,
            }}
          >
            <Form.Item
              name="mobile"
              rules={[
                { required: true, message: '请输入手机号' },
                { pattern: /^1[3578]\d{9}$/, message: '手机号格式错误' },
              ]}
            >
              <Input placeholder="请输入手机号" />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[
                { required: true, message: '请输入验证码' },
                { pattern: /^\w{4,6}$/, message: '验证码格式错误' },
              ]}
            >
              <Input placeholder="请输入验证码" />
            </Form.Item>

            <Form.Item
              name="agree"
              valuePropName="checked"
              rules={[
                {
                  validator: (_, value) => {
                    return value
                      ? Promise.resolve()
                      : Promise.reject(new Error('请勾选用户许可'))
                  },
                },
              ]}
            >
              <Checkbox>我已阅读并同意[用户协议]和[隐私条款]</Checkbox>
            </Form.Item>

            <Form.Item>
              <Button type="primary" htmlType="submit" block>
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    )
  }

  // 提交表单触发的函数
  onFinish = async (values) => {
    if (!this.state.isLogining) {
      this.setState((prestate) => (prestate.isLogining = true))
      store.dispatch({
        type: 'login',
        payload: values,
        props: this.props,
      })
    }
  }
}
